package com.yanghx.transaction.web;

import com.yanghx.transaction.dao.CustomerRepostory;
import com.yanghx.transaction.entity.Customer;
import com.yanghx.transaction.service.CustomerServiceInAnnotation;
import com.yanghx.transaction.service.CustomerServiceInCode;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangHX
 * createTime  2019/8/9 0:21
 */
@RestController
@RequestMapping(value = "/api/customer")
public class CustomerController {
    @Resource
    private JmsTemplate jmsTemplate;
    @Resource
    private CustomerServiceInCode customerServiceInCode;
    @Resource
    private CustomerServiceInAnnotation customerServiceInAnnotation;
    @Resource
    private CustomerRepostory customerRepostory;


    @PostMapping(value = "/annotation")
    public Customer createInAnnotation(@RequestBody Customer customer) {
        return customerServiceInAnnotation.create(customer);
    }

    @PostMapping(value = "/code")
    public Customer createInCode(@RequestBody Customer customer) {
        return customerServiceInCode.create(customer);
    }

    @Transactional
    @PostMapping("/message/annotation")
    public void createMsgWithListener(@RequestParam String userName) {
        jmsTemplate.convertAndSend("customer:msg:new", userName);
    }

    @Transactional
    @PostMapping(value = "/message/code")
    public void createMsgWithDirect(@RequestParam String userName) {
        jmsTemplate.convertAndSend("customer;msg2:new", userName);
    }

    @GetMapping("")
    public List<Customer> getAll() {
        return customerRepostory.findAll();
    }
}
